package com.jysz.struts2.components;

import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.components.Form;
import org.apache.struts2.components.TextField;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;
import org.apache.struts2.views.annotations.StrutsTagSkipInheritance;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * @author <a href="mailto:javaengine@outlook.com">Kaca Rich</a>
 *
 */
@StrutsTag(name="autocompleter", tldTagClass="com.jysz.struts2.views.jsp.ui.AutoCompleterTag", description="Renders a combobox with autocomplete and AJAX capabilities")
public class AutoCompleter extends TextField{
	public static final String TEMPLATE = "autocompleter";
	final private static String COMPONENT_NAME = AutoCompleter.class.getName();
    private final static transient Random RANDOM = new Random();
    private String classType;
    private String dataUrl;
    private String displayTemplate;//TODO：显示的格式，暂时不支持。
    private String displayFields;//逗号分隔
    private String fieldMapping;//{"nameField":"name","realNameField":"realName"}
    private String callBackFun;//回调函数
    private String filterSql;//过滤的sql条件
    private String noSelectedClear; // 未选择清空数据。。默认为false;
    
    public AutoCompleter(ValueStack stack, HttpServletRequest request,
            HttpServletResponse response) {
        super(stack, request, response);
//        CommonService commonService = (CommonService) SpringBeanManager.getBean(CommonService.class.getName());
    }

    public void evaluateExtraParams() {
        super.evaluateExtraParams();
        
        if (classType != null) {
            addParameter("classType", findString(classType));
        }

        if(displayFields != null) {
        	 addParameter("displayFields", findString(displayFields));
        }
        
        if(callBackFun != null){
        	addParameter("callBackFun", findString(callBackFun));
        }    
        
        if(fieldMapping != null){
        	addParameter("fieldMapping", findString(fieldMapping));
        }
        
        if(filterSql != null){
        	addParameter("filterSql", findString(filterSql));
        }
        
        if(noSelectedClear != null){
        	addParameter("noSelectedClear", findValue(noSelectedClear));
        }
//        if (dataUrl != null) {
//            addParameter("dataUrl", findString(dataUrl));
//        }
    }
    
    
    /* (non-Javadoc)
     * @see org.apache.struts2.components.UIBean#populateComponentHtmlId(org.apache.struts2.components.Form)
     */
    protected void populateComponentHtmlId(Form form) {
        String tryId;
        if (id != null) {
            // this check is needed for backwards compatibility with 2.1.x
            tryId = findStringIfAltSyntax(id);
        } else if (form != null) {
            tryId = form.getParameters().get("id") + "_"
                    + escape(name != null ? findString(name) : null);
        } else {
            tryId = escape(name != null ? findString(name) : null);
        }
        
        if(StringUtils.isBlank(tryId)){
            // resolves Math.abs(Integer.MIN_VALUE) issue reported by FindBugs 
            // http://findbugs.sourceforge.net/bugDescriptions.html#RV_ABSOLUTE_VALUE_OF_RANDOM_INT
            int nextInt = RANDOM.nextInt();
            nextInt = nextInt == Integer.MIN_VALUE ? Integer.MAX_VALUE : Math.abs(nextInt);  
            this.id = "widget_" + String.valueOf(nextInt);
            
            tryId = this.id;
        }
        addParameter("id", tryId);
        addParameter("escapedId", escape(tryId));
    }
    protected String getDefaultTemplate() {
        return TEMPLATE;
    }

    public String getComponentName() {
        return COMPONENT_NAME;
    }
    
    @Override
    @StrutsTagSkipInheritance
    public void setTheme(String theme) {
        super.setTheme(theme);
    }
    
    @Override
    public String getTheme() {
        return "xhtml";
    }

	public String getClassType() {
		return classType;
	}

	@StrutsTagAttribute(description="entity class",  type="String", required = true)
	public void setClassType(String classType) {
		this.classType = classType;
	}

	public String getDataUrl() {
		return dataUrl;
	}

	@StrutsTagAttribute(description="data url",  type="String", defaultValue="common/autocomplete.do")
	public void setDataUrl(String dataUrl) {
		this.dataUrl = dataUrl;
	}

	public String getDisplayTemplate() {
		return displayTemplate;
	}

	@StrutsTagAttribute(description="data url",  type="String", defaultValue="%1$s/<span class=\"selectme\">%3$s</span>/%2$s")
	public void setDisplayTemplate(String displayTemplate) {
		this.displayTemplate = displayTemplate;
	}

	public String getDisplayFields() {
		return displayFields;
	}

	@StrutsTagAttribute(description="data url",  type="String", required=true)
	public void setDisplayFields(String displayFields) {
		this.displayFields = displayFields;
	}

	public String getFieldMapping() {
		return fieldMapping;
	}

	@StrutsTagAttribute(description="data url",  type="String")
	public void setFieldMapping(String fieldMapping) {
		this.fieldMapping = fieldMapping;
	}

	public String getCallBackFun() {
		return callBackFun;
	}

	@StrutsTagAttribute(description="data url",  type="String")
	public void setCallBackFun(String callBackFun) {
		this.callBackFun = callBackFun;
	}

	public String getFilterSql() {
		return filterSql;
	}

	@StrutsTagAttribute(description="filterSql",  type="String")
	public void setFilterSql(String filterSql) {
		this.filterSql = filterSql;
	}

	@StrutsTagAttribute(description="filterSql",  type="Boolean", defaultValue="false")
	public String getNoSelectedClear() {
		return noSelectedClear;
	}

	public void setNoSelectedClear(String noSelectedClear) {
		this.noSelectedClear = noSelectedClear;
	}
	
	
}
